package com.rashith.metro.alfa;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

public class GLObject {

//	private float vertices[] = { 
//			1, 1, -1, 	//0
//			1, -1, -1, 	//1
//			-1, -1, -1,	//2
//			-1, 1 , -1, //3 
//			1, 1, 1,   	//4
//			1, -1, 1,	//5
//			-1, -1, 1,	//6
//			-1, 1 , 1,	//7
//	};
	
	private float vertices[] = { 
			0, 0, -2, 	//0
			1, 0.8F, 1, 	//1
			-1, 0.8F, 1,	//2
			1, -0.8F, 1,   //3
			-1, -0.8f, 1,	//4
		
	};

	/*
	private short[] pIndex = { 

			0, 4, 5,    0, 5, 1,
            1, 5, 6,    1, 6, 2,
            2, 6, 7,    2, 7, 3,
            3, 7, 4,    3, 4, 0,
            4, 7, 6,    4, 6, 5,
            3, 0, 1,    3, 1, 2
	};
	*/
	
	 float[] colors = {
	            1f, 0f, 0f, 1f, // point 0 red
	            0f, 1f, 0f, 1f, // point 1 green
	            0f, 1f, 0f, 1f, // point 2 green
	            0f, 0f, 1f, 1f, // point 3 blue
	            0f, 0f, 1f, 1f, // point 4 blue
	    };
	
	private short[] pIndex = {
           0, 1, 2,  0,2,4,
           0,3,1, 	0,4,3,
           2,3,4, 	2,1,3,
    };
	

	private FloatBuffer vertBuff, colorBuff;
	private ShortBuffer sBuff;

	public GLObject() {
		ByteBuffer bBuff = ByteBuffer.allocateDirect(vertices.length * 4);
		bBuff.order(ByteOrder.nativeOrder());
		vertBuff = bBuff.asFloatBuffer();
		vertBuff.put(vertices);
		vertBuff.position(0);

		ByteBuffer pBuff = ByteBuffer.allocateDirect(pIndex.length * 2 );
		pBuff.order(ByteOrder.nativeOrder());
		sBuff = pBuff.asShortBuffer();
		sBuff.put(pIndex);
		sBuff.position(0);
		
		ByteBuffer coBuff =  ByteBuffer.allocateDirect(colors.length * 4);
		coBuff.order(ByteOrder.nativeOrder());
		colorBuff = coBuff.asFloatBuffer();
		colorBuff.put(colors);
		colorBuff.position(0);
	}
	
	public void draw(GL10 gl) {
		gl.glFrontFace(GL10.GL_CW);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuff);
		gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuff);
		gl.glDrawElements(GL10.GL_TRIANGLES, pIndex.length, GL10.GL_UNSIGNED_SHORT, sBuff);
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
	}
}
